package leetcode.code1514;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public double maxProbability(int n, int[][] edges, double[] ss, int start, int end) {
		Map<Integer, Map<Integer, Double>> map = new HashMap<>();
		for (int i = 0; i < edges.length; i++) {
			int a = edges[i][0], b = edges[i][1];
			map.computeIfAbsent(a, key -> new HashMap<>()).put(b, ss[i]);
			map.computeIfAbsent(b, key -> new HashMap<>()).put(a, ss[i]);
		}
		double[] pro = new double[n];
		PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> Double.compare(pro[b], pro[a]));
		pro[start] = 1;
		queue.add(start);
		while (!queue.isEmpty()) {
			int cur = queue.poll();
			if (cur == end) {
				continue;
			}
			double s = pro[cur];
			Map<Integer, Double> nexts = map.getOrDefault(cur, new HashMap<>());
			for (Map.Entry<Integer, Double> entry : nexts.entrySet()) {
				int next = entry.getKey();
				double e = s * entry.getValue();
				if (e > pro[next]) {
					pro[next] = e;
					queue.add(next);
				}
			}
		}
		return pro[end];
	}

	@Override
	public void debug4() {
		double[] arr = new double[] { 0.5, 0.5, 0.2 };
		this.maxProbability(3, H.str2array1("[[0,1],[1,2],[0,2]]"), arr, 0, 2);
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
